package com.lenovo.manufacture.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.adapter.MyAdapter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Fragment11_2 extends Fragment {

    BaseActivity baseActivity;
    @BindView(R.id.rv_order)
    RecyclerView rvOrder;
    List<Map<String, String>> orders;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        baseActivity = ((BaseActivity) getActivity());
        View view = inflater.inflate(R.layout.activity_module11_2, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    public void init() {
        if (baseActivity == null) return;
        baseActivity.async(baseActivity.apiService.getCarType(), data -> {
            List<Map<String, String>> cars = data.getData();
            initOrder(cars);
        });

    }

    private void initOrder(List<Map<String, String>> cars) {
        baseActivity.async(baseActivity.apiService.getAllOrder(), data -> {
            orders = data.getData();
            List<String> strings = new ArrayList<>();
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            orders.forEach(order -> {
                strings.add(cars.stream().filter(car -> car.get("id").equals(order.get("carId"))).collect(Collectors.toList()).get(0).get("carTypeName"));
                strings.add(format.format(new Date(Long.valueOf(order.get("time")) * 1000)));
                strings.add(order.get("num"));
                String engine = "1.0";
                switch (order.get("engine")) {
                    case "1":
                        engine = "1.5";
                        break;
                    case "2":
                        engine = "2.0";
                        break;
                    case "3":
                        engine = "3.0";
                }
                strings.add(engine);
                strings.add(order.get("speed").equals("0") ? "自动" : "手动");
                strings.add(order.get("wheel").equals("0") ? "烤漆" : "电镀");
                strings.add(order.get("control").equals("0") ? "低配" : "高配");
                strings.add(order.get("brake").equals("0") ? "鼓式制动" : "盘式制动");
                String hang = "独立悬挂系统";
                switch (Integer.valueOf(order.get("hang"))) {
                    case 1:
                        hang = "主动悬挂系统";
                        break;
                    case 2:
                        hang = "横臂式悬挂系统";
                        break;
                    case 3:
                        hang = "纵臂式悬挂";
                        break;
                    case 4:
                        hang = "烛式悬挂系统";
                        break;
                    case 5:
                        hang = "多连杆式悬挂系统";
                        break;
                    case 6:
                        hang = "麦佛逊悬挂系统";
                        break;
                }
                strings.add(hang);
            });
            MyAdapter myAdapter = new MyAdapter(strings);
            rvOrder.setLayoutManager(new GridLayoutManager(getContext(), 9));
            rvOrder.setAdapter(myAdapter);
        });
    }

}
